<?php

/*******商品模型（扩展属性）*******/

class GoodsmodelAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(4,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function index(){
		$Model=M('Goods_model');
		$this->model=$Model->select();
		$this->display();
	}
    //添加模型
	public function add(){
		$name=$this->_post('name');
		$Model=M('Goods_model');

		$map['model_name']=$name;
		$result=$Model->where($map)->select();
		if(true==$result){
			$this->error("此模型已经存在！");
		}else{
			$Model->model_name=$name;
			$Model->add();
			$this->success("添加成功！");
		}

	}
   //模型下扩展@字段列表
	public function model_list(){
		$mid=$this->_get('model_id');
		$Model=M('Goods_model_value');
		$Model2=M('Goods_model');
		$mod_list=$Model->where('model_id='.$mid)->select();
		$this->model=$Model2->select();
		for($i=0;$i<count($mod_list);$i++){
			$mod_list[$i]['pdt_key']=implode(',',json_decode($mod_list[$i]['value_key']));
		}

        $this->assign("mod_list",$mod_list);
		$this->assign('choose',$mid);
		$this->display();
	}
    
	//添加模型值（扩展属性）
	public function add_value(){
		$Model=M('Goods_model_value');
		$model=$this->_post('model_id');
		$leixing=$this->_post('leixing');
		$value_name=$this->_post('value_name');
		$value_key=$this->_post('value_key');
		//判断值是否已经存在
		$map['model_id']=$model;
		$map['value_name']=$value_name;
		$check=$Model->where($map)->select();
		if($check) $this->error("已经存在，请更换！");

		//判断添加扩展属性的类型
		if($leixing=='b'||$leixing=='c'){
			$arr=explode(chr(13),$value_key);
			$value_key=str_replace("\\n","",json_encode($arr));
	
			$Model->create();
			$result=$Model->add();

			$Model->value_key=$value_key;
			$rel=$Model->where('id='.$result)->save();
			if($rel) $this->success("添加成功！");
		}else{
			$Model->create();
			$result=$Model->add();
			if($result) $this->success("添加成功");
		}
	}

	//更改模型名
	public function cg_model(){
		$Model=M('Goods_model');
		$mid=$this->_post('mid');
		$mname=$this->_post('mname');
		for($i=0;$i<count($mid);$i++){
			$data['model_name']=$mname[$i];
			$Model->where('model_id='.$mid[$i])->save($data);
		}
	}

	//更改字段值
	public function cg_pdt_value(){
		$Value=M('Goods_model_value');
		$pdt_id=$this->_post('pdt_id');
		$pdt_name=$this->_post('pdt_name');
		$pdt_value=$this->_post('pdt_value');

		for($i=0;$i<count($pdt_id);$i++){
			if(!empty($pdt_value)){
				$data['value_key']=json_encode(explode(',',$pdt_value[$i]));
			}
			$data['value_name']=$pdt_name[$i];
			$Value->where('id='.$pdt_id[$i])->save($data);
		}
	}
    
	//获取模型列表
	public function get_model(){
			$Model=M('Goods_model');
			return $Model->order('model_id desc')->select();
	}

	//添加商品时ajax获取模型扩展字段
	public function get_model_value(){
		//获取选择分类
		$goods_cat=$_POST['goods_cat'];
		$Cat=M('Goods_cat');
		$get['cat_id']=$goods_cat;
		//根据分类得到所属模型
		$mod=$Cat->where($get)->select();

		$Model=M('Goods_model_value');
		$map['model_id']=$mod[0]['model'];
		$kuozhan=$Model->where($map)->select();

        for($i=0;$i<count($kuozhan);$i++){
	         if($kuozhan[$i]['leixing']=='b'){
				 $kuozhan[$i]['value']=json_decode($kuozhan[$i]['value_key']);
	          }
         }
	     $this->kuo=$kuozhan;
		 $this->display();
	}

	//删除整个模型
	public function del_model(){
		$Model=M('Goods_model');
		$Value=M('Goods_model_value');
		$map['model_id']=$this->_get('id');
		$Value->where($map)->delete();
		$Model->where($map)->delete();
	}

	//删除扩展属性
	public function del_pdt(){
		$Value=M('Goods_model_value');
		$map['id']=$this->_get('id');
		$Value->where($map)->delete();
	}



}
?>